<template>
  <div v-show="menuShow" class="canvas-contextmenu" :style="contextMenuStyle">
    <ul @mouseup="handleMouseUp">
      <template v-if="curComponent">
        <template v-if="!curComponent.locked">
          <li @click="copy">复制</li>
          <li @click="paste">粘贴</li>
          <li @click="cut">剪切</li>
          <li @click="deleteComponent">删除</li>
          <li @click="lock">锁定</li>
          <li @click="topComponent">置顶</li>
          <li @click="bottomComponent">置底</li>
          <li @click="upComponent">上移</li>
          <li @click="downComponent">下移</li>
        </template>
        <li v-else @click="unlock">解锁</li>
      </template>
      <li v-else @click="paste">粘贴</li>
    </ul>
  </div>
</template>

<script>
import { computed, defineComponent } from "vue";
import { useDesignStore } from "../../../../../../store/design";

export default defineComponent({
  name: "canvas-contextmenu",
  setup() {
    const designStore = useDesignStore();
    const menuShow = computed(() => designStore.menuShow);
    const menuTop = computed(() => designStore.menuTop);
    const menuLeft = computed(() => designStore.menuLeft);
    const pageConfig = computed(() => designStore.pageConfig);
    const curComponent = computed(() => designStore.curComponent);
    const contextMenuStyle = computed(() => {
      let scale = pageConfig.value.scale
      if (pageConfig.value.scale < 1) {
        scale = 1 / pageConfig.value.scale
      }
      return {
        top: menuTop.value + "px",
        left: menuLeft.value + "px",
        transform: `scale(${scale})`,
        transformOrigin: '0px 0px',
      };
    });
    const copy = () => {
      designStore.copyCurcomponent();
    };
    const paste = (e) => {
      designStore.pasteComponent(true);
      designStore.addSnapshot();
    };
    const cut = () => {
      designStore.cutCurcomponent();
    };
    const deleteComponent = () => {
      designStore.deleteComponent();
      designStore.addSnapshot();
    };
    const lock = () => {
      designStore.setCurcomponentLock();
    };
    const unlock = () => {
      designStore.setCurcomponentUnLock();
    };

    const topComponent = () => {
      designStore.topComponent();
      designStore.addSnapshot();
    };
    const bottomComponent = () => {
      designStore.bottomComponent();
      designStore.addSnapshot();
    };
    const upComponent = () => {
      designStore.upComponent();
      designStore.addSnapshot();
    };
    const downComponent = () => {
      designStore.upComponent();
      designStore.addSnapshot();
    };
    const handleMouseUp = () => {
      designStore.setClickComponentStatus(true);
    };
    return {
      cut,
      lock,
      copy,
      paste,
      unlock,
      menuTop,
      menuLeft,
      menuShow,
      pageConfig,
      topComponent,
      upComponent,
      curComponent,
      downComponent,
      handleMouseUp,
      bottomComponent,
      deleteComponent,
      contextMenuStyle,
    };
  },
});
</script>
<style lang="less" scoped>
.canvas-contextmenu {
  position: absolute;
  z-index: 1000;
  color: #bcc9d4;
  background: #27343e;
  user-select: none;
  ul {
    border-radius: 4px;
    background-color: #27343e;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    margin: 5px 0;
    padding: 6px 0;
    li {
      font-size: 14px;
      padding: 0 20px;
      position: relative;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #fff;
      height: 34px;
      line-height: 34px;
      box-sizing: border-box;
      cursor: pointer;
      &:hover {
        background-color: #1d262e;
        color: #00baff;
        border-left: 2px solid #00baff;
      }
    }
  }
}
</style>